package com.moudle.app.widget.zxing.hanler;

import java.io.UnsupportedEncodingException;

/**
 * author:Lc on 2015/10/13 0013 13:42
 * Content:
 */
public class IsChineseOrNot {

    public static final boolean isChineseCharacter(String chineseStr) {
        char[] charArray = chineseStr.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            //是否是Unicode编码,除了"�"这个字符.这个字符要另外处理
            if ((charArray[i] >= '\u0000' && charArray[i] < '\uFFFD') || ((charArray[i] > '\uFFFD' && charArray[i] < '\uFFFF'))) {
                continue;
            } else {
                return false;
            }
        }
        return true;
    }

    public static final boolean isSpecialCharacter(String str) {
        //是"�"这个特殊字符的乱码情况
        if (str.contains("ï¿½")) {
            return true;
        }
        return false;
    }

    /**
     * @param chaneseStr
     * @return
     */
    public static final String resultString(String chaneseStr) {
        String UTF_Str = "";
        String GB_Str = "";
        boolean is_cN = false;
        try {
            //System.out.println("------------" + chaneseStr);
            UTF_Str = new String(chaneseStr.getBytes("ISO-8859-1"), "UTF-8");
            //System.out.println("这是转了UTF-8的" + UTF_Str);
            is_cN = IsChineseOrNot.isChineseCharacter(UTF_Str);
            //防止有人特意使用乱码来生成二维码来判断的情况
            boolean b = IsChineseOrNot.isSpecialCharacter(chaneseStr);
            if (b) {
                is_cN = true;
            }
            //System.out.println("是为:" + is_cN);
            if (!is_cN) {
                GB_Str = new String(chaneseStr.getBytes("ISO-8859-1"), "GB2312");
                //System.out.println("这是转了GB2312的" + GB_Str);
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (is_cN) {
            return UTF_Str.replaceAll("\r|\n| ", "");
        } else {
            return GB_Str.replaceAll("\r|\n| ", "");
        }
    }

    /** 以"，"进行切割
     * @param chaneseStr
     * @return
     */
    public static final String[] resultSourceStrArray(String chaneseStr) {
        StringBuffer stringBuffer = new StringBuffer(chaneseStr);
        String[] sourceStrArray = stringBuffer.toString().split("，");
        return sourceStrArray;
    }
}